<template>
    <div class="login-bg">
        <div class="login-container">
            <div class="login-card">
                <div class="login-header">
                    <div class="logo-section">
                        <span class="logo-icon">🐰</span>
                        <h1 class="login-title">爱吃兔</h1>
                        <p class="login-subtitle">企业管理系统</p>
                    </div>
                </div>

                <el-form :model="form" label-width="0" class="login-form">
                    <div class="form-group">
                        <div class="input-wrapper">
                            <el-input v-model="form.userName" placeholder="请输入用户名" class="custom-input" size="large"
                                :prefix-icon="User" />
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="input-wrapper">
                            <el-input v-model="form.UserPass" show-password placeholder="请输入密码" class="custom-input"
                                size="large" :prefix-icon="Lock" />
                        </div>
                    </div>

                    <div class="form-group captcha-group">
                        <div class="input-wrapper captcha-input">
                            <el-input v-model="form.Code" placeholder="验证码" class="custom-input" size="large"
                                maxlength="4" :prefix-icon="Key" />
                        </div>
                        <div class="captcha-image-wrapper">
                            <img :src="captchaUrl" :width="width" :height="height" @click="refreshCaptcha"
                                class="captcha-image" :alt="'点击刷新验证码'" title="点击刷新验证码" />
                        </div>
                    </div>

                    <div class="form-actions">
                        <el-button type="primary" @click="save1" class="login-btn" size="large">
                            <el-icon>
                                <Right />
                            </el-icon>
                            登录
                        </el-button>

                        <div class="register-link">
                            <el-text type="primary" @click="save2" class="register-text">
                                <el-icon>
                                    <UserFilled />
                                </el-icon>
                                注册账号
                            </el-text>
                        </div>
                    </div>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { Read, Write } from '@/components/Http/axiosHepler'
import { ElMessage } from 'element-plus';
import { User, Lock, Key, Right, UserFilled } from '@element-plus/icons-vue';
import { useCounterStore } from "@/stores/counter"
import { useRoute, useRouter } from 'vue-router';
import router from '@/router';

const Router = useRouter();
const Route = useRoute();
const store = useCounterStore();
const form = reactive({
    userName: '',
    UserPass: '',
    Code: ''
})

const width = 120;
const height = 40;
const baseUrl = 'https://localhost:7120/api/VerifyCode/GetImageVerifyCodeFile';
const captchaUrl = ref(getCaptchaUrl());

function getCaptchaUrl() {
    // 加上时间戳防止缓存
    return `${baseUrl}?width=${width}&height=${height}&length=4&_t=${Date.now()}`;
}
function refreshCaptcha() {
    captchaUrl.value = getCaptchaUrl();
}

const save1 = () => {
    if (!form.userName) {
        ElMessage.error('用户名不能为空');
        return;
    }
    if (!form.UserPass) {
        ElMessage.error('密码不能为空');
        return;
    }
    if (!form.Code) {
        ElMessage.error('验证码不能为空');
        return;
    }
    // 直接提交，验证码由后端校验
    Read.get(`/Management/UserLogin?UserName=${form.userName}&UserPass=${form.UserPass}&Code=${form.Code}`)
        .then(res => {
            if (res.data.code == 200) {
                store.CurrentLogin.userId = res.data.data.userId
                store.CurrentLogin.userName = res.data.data.userName
                store.CurrentLogin.userNickName = res.data.data.userNickName
                store.CurrentLogin.roleId = res.data.data.roleId
                store.CurrentLogin.roleName = res.data.data.roleName
                store.CurrentLogin.token = res.data.data.token
                ElMessage.success(res.data.message);
                router.push("/Main")
            } else {
                ElMessage.error(res.data.message);
                refreshCaptcha(); // 登录失败刷新验证码
            }
        })
        .catch(() => {
            ElMessage.error('登录失败');
            refreshCaptcha();
        });
}

const save2 = () => {
    router.push("/LoginCreate");
}
</script>

<style scoped>
body {
    display: flex;
    place-items: center;
}

.login-bg {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    z-index: 1;
    overflow: hidden;
}

.login-bg::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="50" cy="50" r="1" fill="rgba(255,255,255,0.1)"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
    opacity: 0.3;
    pointer-events: none;
}

.login-container {
    position: relative;
    z-index: 10;
    width: 100%;
    max-width: 420px;
    padding: 20px;
}

.login-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px);
    border-radius: 24px;
    padding: 48px 40px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.2);
    position: relative;
    overflow: hidden;
}

.login-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
    border-radius: 24px;
    pointer-events: none;
}

.login-header {
    text-align: center;
    margin-bottom: 40px;
}

.logo-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}

.logo-icon {
    font-size: 3rem;
    animation: bounce 2s infinite;
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
}

@keyframes bounce {

    0%,
    20%,
    50%,
    80%,
    100% {
        transform: translateY(0);
    }

    40% {
        transform: translateY(-8px);
    }

    60% {
        transform: translateY(-4px);
    }
}

.login-title {
    font-size: 2.2rem;
    font-weight: 700;
    color: #2c3e50;
    margin: 0;
    letter-spacing: 2px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.login-subtitle {
    font-size: 1rem;
    color: #7f8c8d;
    margin: 0;
    font-weight: 500;
}

.login-form {
    position: relative;
    z-index: 5;
}

.form-group {
    margin-bottom: 24px;
}

.input-wrapper {
    position: relative;
    transition: all 0.3s ease;
}

.input-wrapper:hover {
    transform: translateY(-2px);
}

.custom-input {
    border-radius: 12px;
    transition: all 0.3s ease;
}

.custom-input :deep(.el-input__wrapper) {
    background: rgba(255, 255, 255, 0.8);
    border: 2px solid rgba(64, 158, 255, 0.2);
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
}

.custom-input :deep(.el-input__wrapper:hover) {
    border-color: rgba(64, 158, 255, 0.4);
    box-shadow: 0 6px 16px rgba(64, 158, 255, 0.15);
}

.custom-input :deep(.el-input__wrapper.is-focus) {
    border-color: #409EFF;
    box-shadow: 0 8px 24px rgba(64, 158, 255, 0.25);
}

.custom-input :deep(.el-input__inner) {
    font-size: 1rem;
    padding: 16px 20px;
    color: #2c3e50;
}

.custom-input :deep(.el-input__prefix) {
    color: #409EFF;
    font-size: 1.1rem;
}

.captcha-group {
    display: flex;
    gap: 16px;
    align-items: flex-end;
}

.captcha-input {
    flex: 1;
}

.captcha-image-wrapper {
    flex-shrink: 0;
}

.captcha-image {
    border-radius: 12px;
    border: 2px solid rgba(64, 158, 255, 0.2);
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.captcha-image:hover {
    border-color: #409EFF;
    box-shadow: 0 6px 16px rgba(64, 158, 255, 0.2);
    transform: scale(1.02);
}

.form-actions {
    margin-top: 32px;
}

.login-btn {
    width: 100%;
    height: 52px;
    border-radius: 12px;
    font-size: 1.1rem;
    font-weight: 600;
    background: linear-gradient(135deg, #409EFF 0%, #36a3f7 100%);
    border: none;
    box-shadow: 0 8px 24px rgba(64, 158, 255, 0.3);
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.login-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 32px rgba(64, 158, 255, 0.4);
    background: linear-gradient(135deg, #36a3f7 0%, #409EFF 100%);
}

.register-link {
    text-align: center;
    margin-top: 20px;
}

.register-text {
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: 8px;
}

.register-text:hover {
    background: rgba(64, 158, 255, 0.1);
    transform: translateY(-1px);
}

/* 响应式设计 */
@media (max-width: 480px) {
    .login-container {
        padding: 16px;
    }

    .login-card {
        padding: 32px 24px;
    }

    .login-title {
        font-size: 1.8rem;
    }

    .captcha-group {
        flex-direction: column;
        gap: 12px;
    }

    .captcha-image {
        width: 100%;
        height: auto;
    }
}
</style>